
const express=require('express');
const router=express.Router();


const ProcessingCompany=require("../../models/ProcessingCompany");

router.post("/add",(req,res)=>{
    const name=req.query.name;
    const des=req.query.description;
    ProcessingCompany.findOne({'name': name})
    .then(workshop=>{
        if(workshop){
            return res.json({
                code:400,
                message:'该名称已被使用'
            })
        }
         new ProcessingCompany({
            name, description:des
        }).save()
        .then(workshop=>res.json({
            code:200,
            data:workshop
        }))
        .catch(err=>console.log(err))
    })
})

router.post("/getList",(req,res)=>{
    const limit=req.query.limit;
    const page=req.query.page;
    ProcessingCompany.find({})
    .then(workshops=>{
        const pageList = workshops.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        res.json({
            code:200,
            list:pageList,
            total:workshops.length
        });
    })
})

router.post("/update",(req,res)=>{
    const name=req.query.name;
    const description=req.query.description;
    ProcessingCompany.updateOne({name: name}, {name, description})
    .then(data=>{
        console.log('update',data);
        res.json({
            code:200,
            message:'更新成功'
        });
    })
})

router.post("/delete",(req,res)=>{
    const name=req.query.name;
    ProcessingCompany.deleteOne({name: name})
    .then(data=>{
        console.log('delete',data);
        res.json({
            code:200,
            message:'删除成功'
        });
    })
})

module.exports=router;